<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>前言 | 数据挖掘项目</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/rossmann/assets/logo.jpg">
    <meta name="description" content="Data Mining">
    
    <link rel="preload" href="/rossmann/assets/css/0.styles.96fad13f.css" as="style"><link rel="preload" href="/rossmann/assets/js/app.4768928e.js" as="script"><link rel="preload" href="/rossmann/assets/js/2.46bed51d.js" as="script"><link rel="preload" href="/rossmann/assets/js/12.d00b63a5.js" as="script"><link rel="prefetch" href="/rossmann/assets/js/10.9ada15e0.js"><link rel="prefetch" href="/rossmann/assets/js/11.f29b186a.js"><link rel="prefetch" href="/rossmann/assets/js/13.d8f675b5.js"><link rel="prefetch" href="/rossmann/assets/js/3.c508cade.js"><link rel="prefetch" href="/rossmann/assets/js/4.2be134af.js"><link rel="prefetch" href="/rossmann/assets/js/5.11a4289f.js"><link rel="prefetch" href="/rossmann/assets/js/6.fb1784b8.js"><link rel="prefetch" href="/rossmann/assets/js/7.d79025f2.js"><link rel="prefetch" href="/rossmann/assets/js/8.bc6b450a.js"><link rel="prefetch" href="/rossmann/assets/js/9.a786ec9b.js">
    <link rel="stylesheet" href="/rossmann/assets/css/0.styles.96fad13f.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/rossmann/" class="home-link router-link-active"><!----> <span class="site-name">数据挖掘项目</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/rossmann/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/rossmann/guide/" class="nav-link router-link-active">
  技术文档
</a></div><div class="nav-item"><a href="https://vimeriochen.gitee.io/visualization/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  具体效果
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="http://sim.whu.edu.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情链接
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/rossmann/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/rossmann/guide/" class="nav-link router-link-active">
  技术文档
</a></div><div class="nav-item"><a href="https://vimeriochen.gitee.io/visualization/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  具体效果
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="http://sim.whu.edu.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情链接
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>echarts</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>vue-press</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>数据挖掘</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/rossmann/guide/数据挖掘 一.html" class="active sidebar-link">前言</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#前言" class="sidebar-link">前言</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#正文" class="sidebar-link">正文</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_1-查看数据集信息" class="sidebar-link">1.查看数据集信息</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_2-观察每个特征唯一值数量-对异常值进行处理" class="sidebar-link">2.观察每个特征唯一值数量，对异常值进行处理</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_3-检查缺失列和行情况并对缺失情况做处理" class="sidebar-link">3.检查缺失列和行情况并对缺失情况做处理</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#二-连接表相关表" class="sidebar-link">二.连接表相关表</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#三-探索性数据分析" class="sidebar-link">三.探索性数据分析</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_1-销售额与时间的关系" class="sidebar-link">1.销售额与时间的关系</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_2-促销特征和竞争者特征探索" class="sidebar-link">2.促销特征和竞争者特征探索</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_3-探索节假日对销售情况的影响" class="sidebar-link">3.探索节假日对销售情况的影响</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#_4-探索不同类型商店销售额情况" class="sidebar-link">4.探索不同类型商店销售额情况</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 一.html#总结" class="sidebar-link">总结：</a></li></ul></li><li><a href="/rossmann/guide/数据挖掘 二.html" class="sidebar-link">前言</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="前言"><a href="#前言" class="header-anchor">#</a> <strong>前言</strong></h2> <p><strong>项目描述</strong></p> <p>Rossmann在7个欧洲国家经营着3,000多家日用店。目前，Rossmann商店经理的任务是预先提前六周预测他们的日常销售。商店销售受许多因素的影响，包括促销，竞争，学校和州假日，季节性和地方性。成千上万的个体经理根据他们独特的情况预测销售情况，结果的准确性可能会有很大差异。本次项目要求预测德国各地的1,115家商店6周中每周销售额。</p> <p><strong>分析过程描述</strong></p> <p>本案例数据来源kaggle的<a href="https://link.zhihu.com/?target=https%3A//www.kaggle.com/c/rossmann-store-sales" target="_blank" rel="noopener noreferrer">Rossmann Store Sales | Kaggle<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，数据量达到100多万。一共三个表分别为**train.csv，store.csv，test.csv。**由于篇幅较长，本次项目分两部分，第一部分为数据处理和探索性数据分析，第二部分为特征处理和建模。</p> <p>首先通过对数据集处理，包括异常值处理，缺失值处理等；其次通过eda对数据进行探索，分析各特征对销售额情况的影响，并且针对探索结果给出相应的运营建议；然后通过特征工程包括onehot处理，标准化处理，将特征数据转化为模型可以测试的数据。最后建模过程，本次案例主要用到的是线性<strong>回归</strong>，<strong>决策树</strong>和<strong>随机森林</strong>和<strong>GBDT</strong>四类回归算法。</p> <p><strong>数据集描述（只对有必要说明的做解释）</strong></p> <ul><li><strong>Id</strong> - 表示测试集中的（存储，日期）重复的Id</li> <li><strong>Store</strong>- 每家<strong>商店</strong>的唯一ID</li> <li><strong>Sales</strong> - 任何一天的营业额（这是您预测的）</li> <li><strong>Customers</strong>- 特定日期的客户数量</li> <li><strong>Open</strong> - 商店是否<strong>营业</strong>的指标：0 =关闭，1 =打开</li> <li><strong>StateHoliday</strong> - 表示州假日。通常情况下，除了少数例外，所有商店都会在州假期关闭。请注意，所有学校在公众假期和周末都关闭。a =公共假日，b =复活节假期，c =圣诞节，0 =无</li> <li><strong>SchoolHoliday</strong> - 表示（商店，日期）是否受到公立学校关闭的影响</li> <li><strong>StoreType</strong> - 区分4种不同的商店模型：a，b，c，d</li> <li><strong>Assortment</strong>- 描述分类级别：a =基本，b =额外，c =扩展</li> <li><strong>CompetitionDistance</strong> - 距离最近的竞争对手商店的距离（以米为单位）</li> <li><strong>CompetitionOpenSince [Month / Year]</strong> - 给出最接近的竞争对手开放时间的大致年份和月份</li> <li><strong>Promo</strong>- 指示商店当天是否正在运营促销</li> <li><strong>Promo2</strong> - Promo2是一些商店的持续和连续促销：0 =商店不参与，1 =商店参与</li> <li><strong>Promo2Since [年/周]</strong> - 描述商店开始参与Promo2的年份和日历周</li> <li><strong>PromoInterval</strong> - 描述Promo2启动的连续间隔，命名重新开始促销的月份。例如，“2月，5月，8月，11月”意味着每一轮在该商店的任何给定年份的2月，5月，8月和11月开始</li></ul> <h2 id="正文"><a href="#正文" class="header-anchor">#</a> <strong>正文</strong></h2> <p><strong>一.导入与查看数据集情况</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns
<span class="token operator">%</span>matplotlib inline
<span class="token keyword">def</span> <span class="token function">open_csv</span><span class="token punctuation">(</span>csv_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">r'C:\Users\Administrator.DESKTOP-ULJ84AO\Desktop\1\{}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>csv_name<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
        data<span class="token operator">=</span>pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>f<span class="token punctuation">)</span>
    <span class="token keyword">return</span> data
data_store<span class="token operator">=</span>open_csv<span class="token punctuation">(</span><span class="token string">'store.csv'</span><span class="token punctuation">)</span>
data_train<span class="token operator">=</span>open_csv<span class="token punctuation">(</span><span class="token string">'train.csv'</span><span class="token punctuation">)</span>
data_test<span class="token operator">=</span>open_csv<span class="token punctuation">(</span><span class="token string">'test.csv'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_1-查看数据集信息"><a href="#_1-查看数据集信息" class="header-anchor">#</a> 1.查看数据集信息</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">check_file</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>data_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'{}前3行'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>data_name<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'{}各行信息'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>data_name<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'\n'</span><span class="token punctuation">,</span><span class="token string">'\n'</span><span class="token punctuation">)</span>
datas<span class="token operator">=</span><span class="token punctuation">[</span>data_store<span class="token punctuation">,</span>data_train<span class="token punctuation">,</span>data_test<span class="token punctuation">]</span>
data_names<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'data_store'</span><span class="token punctuation">,</span><span class="token string">'data_train'</span><span class="token punctuation">,</span><span class="token string">'data_test'</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> data<span class="token punctuation">,</span>data_name <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>datas<span class="token punctuation">,</span>data_names<span class="token punctuation">)</span><span class="token punctuation">:</span>
    check_file<span class="token punctuation">(</span>data<span class="token punctuation">,</span>data_name<span class="token punctuation">)</span>



<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_store前<span class="token number">3</span>行<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span>
   Store StoreType Assortment  CompetitionDistance  CompetitionOpenSinceMonth  \
<span class="token number">0</span>      <span class="token number">1</span>         c          a               <span class="token number">1270.0</span>                        <span class="token number">9.0</span>   
<span class="token number">1</span>      <span class="token number">2</span>         a          a                <span class="token number">570.0</span>                       <span class="token number">11.0</span>   
<span class="token number">2</span>      <span class="token number">3</span>         a          a              <span class="token number">14130.0</span>                       <span class="token number">12.0</span>

   CompetitionOpenSinceYear  Promo2  Promo2SinceWeek  Promo2SinceYear  \
<span class="token number">0</span>                    <span class="token number">2008.0</span>       <span class="token number">0</span>              NaN              NaN   
<span class="token number">1</span>                    <span class="token number">2007.0</span>       <span class="token number">1</span>             <span class="token number">13.0</span>           <span class="token number">2010.0</span>   
<span class="token number">2</span>                    <span class="token number">2006.0</span>       <span class="token number">1</span>             <span class="token number">14.0</span>           <span class="token number">2011.0</span>

     PromoInterval  
<span class="token number">0</span>              NaN  
<span class="token number">1</span>  Jan<span class="token punctuation">,</span>Apr<span class="token punctuation">,</span>Jul<span class="token punctuation">,</span>Oct  
<span class="token number">2</span>  Jan<span class="token punctuation">,</span>Apr<span class="token punctuation">,</span>Jul<span class="token punctuation">,</span>Oct  
<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_store各行信息<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>
<span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token string">'pandas.core.frame.DataFrame'</span><span class="token operator">&gt;</span>
RangeIndex<span class="token punctuation">:</span> <span class="token number">1115</span> entries<span class="token punctuation">,</span> <span class="token number">0</span> to <span class="token number">1114</span>
Data columns <span class="token punctuation">(</span>total <span class="token number">10</span> columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
Store                        <span class="token number">1115</span> non<span class="token operator">-</span>null int64
StoreType                    <span class="token number">1115</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
Assortment                   <span class="token number">1115</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
CompetitionDistance          <span class="token number">1112</span> non<span class="token operator">-</span>null float64
CompetitionOpenSinceMonth    <span class="token number">761</span> non<span class="token operator">-</span>null float64
CompetitionOpenSinceYear     <span class="token number">761</span> non<span class="token operator">-</span>null float64
Promo2                       <span class="token number">1115</span> non<span class="token operator">-</span>null int64
Promo2SinceWeek              <span class="token number">571</span> non<span class="token operator">-</span>null float64
Promo2SinceYear              <span class="token number">571</span> non<span class="token operator">-</span>null float64
PromoInterval                <span class="token number">571</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
dtypes<span class="token punctuation">:</span> float64<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> int64<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">object</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
memory usage<span class="token punctuation">:</span> <span class="token number">87.2</span><span class="token operator">+</span> KB
<span class="token boolean">None</span>


<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_train前<span class="token number">3</span>行<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span>
   Store  DayOfWeek        Date  Sales  Customers  Open  Promo StateHoliday  \
<span class="token number">0</span>      <span class="token number">1</span>          <span class="token number">5</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">07</span><span class="token operator">-</span><span class="token number">31</span>   <span class="token number">5263</span>        <span class="token number">555</span>     <span class="token number">1</span>      <span class="token number">1</span>            <span class="token number">0</span>   
<span class="token number">1</span>      <span class="token number">2</span>          <span class="token number">5</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">07</span><span class="token operator">-</span><span class="token number">31</span>   <span class="token number">6064</span>        <span class="token number">625</span>     <span class="token number">1</span>      <span class="token number">1</span>            <span class="token number">0</span>   
<span class="token number">2</span>      <span class="token number">3</span>          <span class="token number">5</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">07</span><span class="token operator">-</span><span class="token number">31</span>   <span class="token number">8314</span>        <span class="token number">821</span>     <span class="token number">1</span>      <span class="token number">1</span>            <span class="token number">0</span>

   SchoolHoliday  
<span class="token number">0</span>              <span class="token number">1</span>  
<span class="token number">1</span>              <span class="token number">1</span>  
<span class="token number">2</span>              <span class="token number">1</span>  
<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_train各行信息<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>
<span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token string">'pandas.core.frame.DataFrame'</span><span class="token operator">&gt;</span>
RangeIndex<span class="token punctuation">:</span> <span class="token number">1017209</span> entries<span class="token punctuation">,</span> <span class="token number">0</span> to <span class="token number">1017208</span>
Data columns <span class="token punctuation">(</span>total <span class="token number">9</span> columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
Store            <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
DayOfWeek        <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
Date             <span class="token number">1017209</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
Sales            <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
Customers        <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
Open             <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
Promo            <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
StateHoliday     <span class="token number">1017209</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
SchoolHoliday    <span class="token number">1017209</span> non<span class="token operator">-</span>null int64
dtypes<span class="token punctuation">:</span> int64<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">object</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
memory usage<span class="token punctuation">:</span> <span class="token number">69.8</span><span class="token operator">+</span> MB
<span class="token boolean">None</span>


<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_test前<span class="token number">3</span>行<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span>
   Id  Store  DayOfWeek        Date  Open  Promo StateHoliday  SchoolHoliday
<span class="token number">0</span>   <span class="token number">1</span>      <span class="token number">1</span>          <span class="token number">4</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">17</span>   <span class="token number">1.0</span>      <span class="token number">1</span>            <span class="token number">0</span>              <span class="token number">0</span>
<span class="token number">1</span>   <span class="token number">2</span>      <span class="token number">3</span>          <span class="token number">4</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">17</span>   <span class="token number">1.0</span>      <span class="token number">1</span>            <span class="token number">0</span>              <span class="token number">0</span>
<span class="token number">2</span>   <span class="token number">3</span>      <span class="token number">7</span>          <span class="token number">4</span>  <span class="token number">2015</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">17</span>   <span class="token number">1.0</span>      <span class="token number">1</span>            <span class="token number">0</span>              <span class="token number">0</span>
<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>data_test各行信息<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>
<span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token string">'pandas.core.frame.DataFrame'</span><span class="token operator">&gt;</span>
RangeIndex<span class="token punctuation">:</span> <span class="token number">41088</span> entries<span class="token punctuation">,</span> <span class="token number">0</span> to <span class="token number">41087</span>
Data columns <span class="token punctuation">(</span>total <span class="token number">8</span> columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
Id               <span class="token number">41088</span> non<span class="token operator">-</span>null int64
Store            <span class="token number">41088</span> non<span class="token operator">-</span>null int64
DayOfWeek        <span class="token number">41088</span> non<span class="token operator">-</span>null int64
Date             <span class="token number">41088</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
Open             <span class="token number">41077</span> non<span class="token operator">-</span>null float64
Promo            <span class="token number">41088</span> non<span class="token operator">-</span>null int64
StateHoliday     <span class="token number">41088</span> non<span class="token operator">-</span>null <span class="token builtin">object</span>
SchoolHoliday    <span class="token number">41088</span> non<span class="token operator">-</span>null int64
dtypes<span class="token punctuation">:</span> float64<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> int64<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">object</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
memory usage<span class="token punctuation">:</span> <span class="token number">2.5</span><span class="token operator">+</span> MB
<span class="token boolean">None</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br></div></div><h2 id="_2-观察每个特征唯一值数量-对异常值进行处理"><a href="#_2-观察每个特征唯一值数量-对异常值进行处理" class="header-anchor">#</a> 2.观察每个特征唯一值数量，对异常值进行处理</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">check_unique</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> column <span class="token keyword">in</span> data<span class="token punctuation">.</span>columns<span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}为一值数量:'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>column<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token builtin">len</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#检查store表</span>
check_unique<span class="token punctuation">(</span>data_store<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>Store为一值数量: 1115
StoreType为一值数量: 4
Assortment为一值数量: 3
CompetitionDistance为一值数量: 655
CompetitionOpenSinceMonth为一值数量: 13
CompetitionOpenSinceYear为一值数量: 24
Promo2为一值数量: 2
Promo2SinceWeek为一值数量: 25
Promo2SinceYear为一值数量: 8
PromoInterval为一值数量: 4</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#发现月份有13个，检查</span>
np<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>data_store<span class="token punctuation">[</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">8</span><span class="token punctuation">.</span><span class="token punctuation">,</span>  <span class="token number">9</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nan<span class="token punctuation">]</span><span class="token punctuation">)</span>
np<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>data_store<span class="token punctuation">[</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>array([1900., 1961., 1990., 1994., 1995., 1998., 1999., 2000., 2001.,
2002., 2003., 2004., 2005., 2006., 2007., 2008., 2009., 2010.,
2011., 2012., 2013., 2014., 2015., nan])</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#检查训练数据集表</span>
check_unique<span class="token punctuation">(</span>data_train<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Store为一值数量: 1115
DayOfWeek为一值数量: 7
Date为一值数量: 942
Sales为一值数量: 21734
Customers为一值数量: 4086
Open为一值数量: 2
Promo为一值数量: 2
StateHoliday为一值数量: 5
SchoolHoliday为一值数量: 2</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#检查测试数据集表</span>
check_unique<span class="token punctuation">(</span>data_test<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Id为一值数量: 41088
Store为一值数量: 856
DayOfWeek为一值数量: 7
Date为一值数量: 48
Open为一值数量: 3
Promo为一值数量: 2
StateHoliday为一值数量: 2
SchoolHoliday为一值数量: 2</p> <p><strong>发现StateHoliday有5个，异常，检验处理以下</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train<span class="token punctuation">.</span>StateHoliday<span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>array(['0', 'a', 'b', 'c', 0], dtype=object)</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#字符0和数字重叠</span>
data_train<span class="token punctuation">.</span>StateHoliday<span class="token operator">=</span>data_train<span class="token punctuation">.</span>StateHoliday<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'0'</span><span class="token punctuation">:</span><span class="token string">'无'</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token string">'无'</span><span class="token punctuation">,</span><span class="token string">'a'</span><span class="token punctuation">:</span><span class="token string">'公共假日'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">:</span><span class="token string">'复活节假期'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">:</span><span class="token string">'圣诞节'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
data_train<span class="token punctuation">.</span>StateHoliday<span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>array(['无', '公共假日', '复活节假期', '圣诞节'], dtype=object)</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_test<span class="token punctuation">.</span>StateHoliday<span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>array(['0', 'a'], dtype=object)</p> <p><strong>data_test的StateHoliday没问题</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#观察data_test Open有三个值，需要检查</span>
data_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>unique<span class="token punctuation">(</span><span class="token punctuation">)</span>
array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nan<span class="token punctuation">,</span>  <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>对于空值后面再处理</p> <h2 id="_3-检查缺失列和行情况并对缺失情况做处理"><a href="#_3-检查缺失列和行情况并对缺失情况做处理" class="header-anchor">#</a> 3.检查缺失列和行情况并对缺失情况做处理</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">check_none</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'有空值的列'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">any</span><span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'有空值的行数量'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">any</span><span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
check_none<span class="token punctuation">(</span>data_store<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong><strong><strong><strong><strong><strong>有空值的列</strong></strong></strong></strong></strong></strong>*
Store False
StoreType False
Assortment False
CompetitionDistance True
CompetitionOpenSinceMonth True
CompetitionOpenSinceYear True
Promo2 False
Promo2SinceWeek True
Promo2SinceYear True
PromoInterval True
dtype: bool
<em><strong><strong><strong><strong><strong>有空值的行数量</strong></strong></strong></strong></strong></em>*
750</p> <p>check_none(data_train)</p> <p><strong><strong><strong><strong><strong><strong>有空值的列</strong></strong></strong></strong></strong></strong>*
Store False
DayOfWeek False
Date False
Sales False
Customers False
Open False
Promo False
StateHoliday False
SchoolHoliday False
dtype: bool
<em><strong><strong><strong><strong><strong>有空值的行数量</strong></strong></strong></strong></strong></em>*
0</p> <p><strong>发现store数据集有缺失，猜测没有竞争对手，所以相关竞争对手距离等信息没有记录；而prom2为0，相关prom2信息包括Promo2SinceWeek等为空，以下进行验证。</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#检验相关列空值都在同一行</span>
<span class="token comment">#注意，该函数的参数columns必须包含三列</span>
<span class="token keyword">def</span> <span class="token function">check_none_together</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
    indexes<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    indexes_len<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> column <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
        index<span class="token operator">=</span>data<span class="token punctuation">[</span>data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>index<span class="token comment">#找出每列有空值的行索引</span>
        indexes_len<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#算出每列缺失行数量</span>
        indexes<span class="token punctuation">.</span>append<span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token comment">#将每列缺失行索引保留</span>
    <span class="token keyword">if</span> <span class="token builtin">set</span><span class="token punctuation">(</span>indexes<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token builtin">set</span><span class="token punctuation">(</span>indexes<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token builtin">set</span><span class="token punctuation">(</span>indexes<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token builtin">set</span><span class="token punctuation">(</span>indexes<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token comment">#检查每列缺失的行索引是否都一样</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'相同行都为空值'</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>indexes_len<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>indexes_len<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>indexes_len<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">#不一样，则把每个列缺失行数打印出来</span>
    <span class="token keyword">return</span> indexes

<span class="token comment">#对竞争信息为空的验证</span>
index_1<span class="token operator">=</span>check_none_together<span class="token punctuation">(</span>data_store<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token number">3</span> <span class="token number">354</span> <span class="token number">354</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>虽然竞争信息的行空值并非都相同，但CompetitionOpenSinceMonth，CompetitionOpenSinceYear空值行数相同，可以检验是否同行</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>index_2<span class="token operator">=</span>check_none_together<span class="token punctuation">(</span>data_store<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

相同行都为空值
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>以上猜想得到验证，'CompetitionOpenSinceYear','CompetitionOpenSinceMonth'两列缺失行一样</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看CompetitionDistance为空情况</span>
data_store<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span>index_1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029190907014.png" alt="image-20211029190907014"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看CompetitionOpenSinceYear为空某些行情况，'CompetitionOpenSinceMonth'类似</span>
data_store<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span>index_1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>sample<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029190847281.png" alt="image-20211029190847281"></p> <p>得出结论除了没有竞争对手情况，其余CompetitionOpenSinceMonth，CompetitionOpenSinceYear空值情况应该是没有记录，但实际存在相关数据</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对于promo2相关时间数据缺失，猜想promo2是0（即没有促销类型2）的其相应时间数据都是空</span>
<span class="token comment">#检验空值行的Promo2，'Promo2SinceWeek','Promo2SinceYear','PromoInterval'是否一样</span>
indexes_2<span class="token operator">=</span>check_none_together<span class="token punctuation">(</span>data_store<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'Promo2SinceWeek'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceYear'</span><span class="token punctuation">,</span><span class="token string">'PromoInterval'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token builtin">sum</span><span class="token punctuation">(</span>data_store<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span>indexes_2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'Promo2'</span><span class="token punctuation">]</span><span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">#检验空值行的Promo2是否都为0</span>

相同行都为空值

<span class="token number">0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>得出结论Promo2=0的prom2相关列的值都是空值</p> <h2 id="二-连接表相关表"><a href="#二-连接表相关表" class="header-anchor">#</a> <strong>二.连接表相关表</strong></h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train_new<span class="token operator">=</span>pd<span class="token punctuation">.</span>merge<span class="token punctuation">(</span>data_train<span class="token punctuation">,</span>data_store<span class="token punctuation">,</span>on<span class="token operator">=</span><span class="token string">'Store'</span><span class="token punctuation">,</span>how<span class="token operator">=</span><span class="token string">'left'</span><span class="token punctuation">)</span>
data_train_new<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191105329.png" alt="image-20211029191105329"></p> <p><strong>将时间数据转化为时间类型</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train_new<span class="token punctuation">.</span>Date<span class="token operator">=</span>pd<span class="token punctuation">.</span>to_datetime<span class="token punctuation">(</span>data_train_new<span class="token punctuation">.</span>Date<span class="token punctuation">)</span>
data_train_new<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Int64Index: 1017209 entries, 0 to 1017208
Data columns (total 18 columns):
Store 1017209 non-null int64
DayOfWeek 1017209 non-null int64
Date 1017209 non-null datetime64[ns]
Sales 1017209 non-null int64
Customers 1017209 non-null int64
Open 1017209 non-null int64
Promo 1017209 non-null int64
StateHoliday 1017209 non-null object
SchoolHoliday 1017209 non-null int64
StoreType 1017209 non-null object
Assortment 1017209 non-null object
CompetitionDistance 1014567 non-null float64
CompetitionOpenSinceMonth 693861 non-null float64
CompetitionOpenSinceYear 693861 non-null float64
Promo2 1017209 non-null int64
Promo2SinceWeek 509178 non-null float64
Promo2SinceYear 509178 non-null float64
PromoInterval 509178 non-null object
dtypes: datetime(64), float64(5), int64(8), object(4)
memory usage: 147.5+ MB</p> <h2 id="三-探索性数据分析"><a href="#三-探索性数据分析" class="header-anchor">#</a> <strong>三.探索性数据分析</strong></h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#每家店sales总额的分布</span>
df1<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'Store'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">10000</span>
sns<span class="token punctuation">.</span>distplot<span class="token punctuation">(</span>df1<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'每家店sales总额的分布'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>xlabel<span class="token punctuation">(</span><span class="token string">'销售总额/万元'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191139829.png" alt="image-20211029191139829"></p> <p>看出分布是正态分布，多数店销售总额在500万美元，统计的是数据集中所有年总和</p> <h2 id="_1-销售额与时间的关系"><a href="#_1-销售额与时间的关系" class="header-anchor">#</a> 1.销售额与时间的关系</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#一周不同的天平均每天每个店的销售情况</span>
<span class="token comment">#sns.pointplot(data=data_train_new[::100],x='DayOfWeek',y='Sales') 让sns作groupby统计耗时非常长，所以需要先group不要做处理</span>
df2<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>pointplot<span class="token punctuation">(</span>data<span class="token operator">=</span>df2<span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'一周不同的天平均每天每个店的销售情况'</span><span class="token punctuation">)</span>
Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'一周不同的天平均每天每个店的销售情况'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191201284.png" alt="image-20211029191201284"></p> <p>看出周日几乎为0，估计是多数店都不营业，而周一最多，也是因为不营业的后会产生比较大的需求变动。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#一周的平均营业率</span>
df3<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Open'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>rename<span class="token punctuation">(</span>columns<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'Open'</span><span class="token punctuation">:</span><span class="token string">'open_rate'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>pointplot<span class="token punctuation">(</span>data<span class="token operator">=</span>df3<span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'open_rate'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'一周的平均营业率'</span><span class="token punctuation">)</span>
Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'一周的平均营业率'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191219881.png" alt="image-20211029191219881"></p> <p>验证了多数店周日不营业</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#探索每个月平均销售情况</span>
df4<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'Date'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>resample<span class="token punctuation">(</span><span class="token string">'M'</span><span class="token punctuation">,</span>kind<span class="token operator">=</span><span class="token string">'period'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#resample能够把聚合时间变成月份，去除日</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>pointplot<span class="token punctuation">(</span>data<span class="token operator">=</span>df4<span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Date'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>xticks<span class="token punctuation">(</span>rotation<span class="token operator">=</span><span class="token number">45</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'每月平均销售额情况'</span><span class="token punctuation">)</span>
Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'每月平均销售额情况'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191243164.png" alt="image-20211029191243164"></p> <p>可以看出每年10月份—12月份是是快速增长时期，12到峰值，1月到谷底。并且整体来看波动也是比较明显的，不同的年波动情况也有差别</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对data_train_new日期进行年，月，日进行处理</span>
data_train_new<span class="token punctuation">[</span><span class="token string">'year'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>year<span class="token punctuation">)</span>
data_train_new<span class="token punctuation">[</span><span class="token string">'month'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>month<span class="token punctuation">)</span>
data_train_new<span class="token punctuation">[</span><span class="token string">'day'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>day<span class="token punctuation">)</span>
data_train_new<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191322856.png" alt="image-20211029191322856"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#不同年每个月份平均销售情况</span>
df5<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>Sales<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>year<span class="token punctuation">,</span>data_train_new<span class="token punctuation">.</span>month<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>pointplot<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token string">'month'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>hue<span class="token operator">=</span><span class="token string">'year'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df5<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'不同年每个月份平均销售情况'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029191343340.png" alt="image-20211029191343340"></p> <p>看出销售额与月份的波动情况相似，并且10月份到12月份销售额增长较快，而到1月份会快速将会原有平均水平</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#不同月份每天平均销售情况</span>
df6<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>Sales<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>month<span class="token punctuation">,</span>data_train_new<span class="token punctuation">.</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>pointplot<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token string">'day'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>hue<span class="token operator">=</span><span class="token string">'month'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df6<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'不同月份每天平均销售情况'</span><span class="token punctuation">)</span>

<span class="token comment">#标记12月份最高值与最低值</span>
dec_max<span class="token operator">=</span>df6<span class="token punctuation">[</span>df6<span class="token punctuation">.</span>month<span class="token operator">==</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Sales<span class="token punctuation">.</span><span class="token builtin">max</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
dec_min<span class="token operator">=</span>df6<span class="token punctuation">[</span>df6<span class="token punctuation">.</span>month<span class="token operator">==</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Sales<span class="token punctuation">.</span><span class="token builtin">min</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>annotate<span class="token punctuation">(</span><span class="token string">'12月份最高值'</span><span class="token punctuation">,</span>xy<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">,</span>dec_max<span class="token punctuation">)</span><span class="token punctuation">,</span>xytext<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">17</span><span class="token punctuation">,</span>dec_max<span class="token punctuation">)</span><span class="token punctuation">,</span>arrowprops<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'facecolor'</span><span class="token punctuation">:</span><span class="token string">'b'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment">#xy中横坐标第一个数为1，所以填写的横坐标和图上的差一</span>
plt<span class="token punctuation">.</span>annotate<span class="token punctuation">(</span><span class="token string">'12月份最低值'</span><span class="token punctuation">,</span>xy<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span>dec_min<span class="token punctuation">)</span><span class="token punctuation">,</span>xytext<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span>dec_min<span class="token operator">+</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>arrowprops<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'facecolor'</span><span class="token punctuation">:</span><span class="token string">'b'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><img src="C:%5CUsers%5CAdministrator.DESKTOP-ULJ84AO%5CDesktop%5C%E4%B8%8B%E8%BD%BD.png" alt="下载"></p> <p>可以看出12月份波动最为剧烈，纵观12月也只是16号到26号间和其他月份有较达差别，说明12月份总体比其他月份高是由于这段时间的巨大波动变化引起的，值得深思。其他月份波动范围相似</p> <h2 id="_2-促销特征和竞争者特征探索"><a href="#_2-促销特征和竞争者特征探索" class="header-anchor">#</a> 2.促销特征和竞争者特征探索</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#探索竞争者距离和销售额关系</span>
df7<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'Store'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">,</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>agg<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">:</span><span class="token string">'mean'</span><span class="token punctuation">,</span><span class="token string">'Sales'</span><span class="token punctuation">:</span><span class="token string">'sum'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment">#每个店销售总额</span>
df7<span class="token punctuation">[</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">]</span><span class="token operator">=</span>df7<span class="token punctuation">[</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>np<span class="token punctuation">.</span>log<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#对距离取log，收缩分布</span>
df7<span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token operator">=</span>df7<span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token operator">/</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token comment">#销售额以万元为单位</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>regplot<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df7<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'竞争者距离和销售额关系'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192118337.png" alt="image-20211029192118337"></p> <p>可以看出在一定的距离范围中，有不少商店会获得比其他店更高的销售额</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#店铺促销次数比例分布</span>
df7<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'Store'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Promo'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>distplot<span class="token punctuation">(</span>df7<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192135018.png" alt="image-20211029192135018"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>df8<span class="token operator">=</span>df7<span class="token punctuation">.</span>value_counts<span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>barplot<span class="token punctuation">(</span>df8<span class="token punctuation">.</span>index<span class="token punctuation">,</span>df8<span class="token punctuation">.</span>values<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'店铺促销次数分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'店铺促销次数分布'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192151521.png" alt="image-20211029192151521"></p> <p>可以看出店铺促销次数只有两个，所以说明店铺促销遵照两种不同规则</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对比促销天和非促销天销售分布情况</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Promo'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'促销天和非促销天销售分布情况'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'促销天和非促销天销售分布情况'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192206210.png" alt="image-20211029192206210"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对比促销天和非促销天销售分布情况(一天销售额超过20000的)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Promo'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>Sales<span class="token operator">&gt;=</span><span class="token number">20000</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'促销天和非促销天销售分布情况'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'促销天和非促销天销售分布情况'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192221294.png" alt="image-20211029192221294"></p> <p>可以发现，当天促销在销售额不超过20000时促销明显比非促销有更高的销售，然而对于销售额超过20000的，则促销与非促销形式就影响不大了。这说明促销对那些靠靠口碑等有着稳定忠诚客户的商店印象不大，而对那些没有忠诚顾客则可以通过促销活动短期内吸引更多关注。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#有无promo2的店数量对比</span>
df9<span class="token operator">=</span>data_store<span class="token punctuation">.</span>Promo2<span class="token punctuation">.</span>value_counts<span class="token punctuation">(</span><span class="token punctuation">)</span>
df9<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>kind<span class="token operator">=</span><span class="token string">'bar'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192237898.png" alt="image-20211029192237898"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#有无promo2的每天每个店销售情况对比</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Promo2'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'有无promo2的每天每个店销售情况对比'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'有无promo2的每天每个店销售情况对比'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192251931.png" alt="image-20211029192251931"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#有无promo2的店铺的平均每天销售情况对比</span>
df10<span class="token operator">=</span>data_train_new<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token string">'Store'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Promo2'</span><span class="token punctuation">,</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Promo2'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df10<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'有无promo2的店铺的平均每天销售情况对比'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Promo2'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df10<span class="token punctuation">[</span>df10<span class="token punctuation">.</span>Sales<span class="token operator">&gt;=</span><span class="token number">10000</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'有无promo2的店铺的平均每天销售情况对比(平均销售额大于10000的店)'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'有无promo2的店铺的平均每天销售情况对比(平均销售额大于10000的店)'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192305547.png" alt="image-20211029192305547"></p> <p>看出有promo2的店平均销售情况比没有稍微好一点，而对于那些平均销售额大于10000的店，没有促销类型2的店销售额整体更好，这说明不以促销为主的店可能有更好的保留忠诚客户的方法，结合实际业务可以对这些店的运营模式做进一步分析来找出原因。</p> <h2 id="_3-探索节假日对销售情况的影响"><a href="#_3-探索节假日对销售情况的影响" class="header-anchor">#</a> 3.探索节假日对销售情况的影响</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对比stateholiday销售分布情况</span>
fig<span class="token punctuation">,</span>ax<span class="token operator">=</span>plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span>sharey<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#不是国家节假日的销售分布</span>
sns<span class="token punctuation">.</span>violinplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'StateHoliday'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>StateHoliday<span class="token operator">==</span><span class="token string">'无'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'不是国家节假日的销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">#国家节假日的销售分布</span>
sns<span class="token punctuation">.</span>violinplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'StateHoliday'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>StateHoliday<span class="token operator">!=</span><span class="token string">'无'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'国家节假日的销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">#国家节假日的销售分布(营业的店)</span>
sns<span class="token punctuation">.</span>violinplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'StateHoliday'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">[</span><span class="token punctuation">(</span>data_train_new<span class="token punctuation">.</span>StateHoliday<span class="token operator">!=</span><span class="token string">'无'</span><span class="token punctuation">)</span><span class="token operator">&amp;</span><span class="token punctuation">(</span>data_train_new<span class="token punctuation">.</span>Sales<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'国家节假日的销售分布(营业的店)'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192327616.png" alt="image-20211029192327616"></p> <p>节假日基本没有销售额,这应该是多数店都关门，而对于营业的店，其分布也和非节假日的分布差不多，由于节假日数据相对较少，相对非节假日的分布也更分散点。可以看出节假日如果营业，对于销售额的影响不是很大。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对比SchoolHoliday销售分布情况</span>

fig<span class="token punctuation">,</span>ax<span class="token operator">=</span>plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#SchoolHoliday的销售分布</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'SchoolHoliday'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'SchoolHoliday的销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">#SchoolHoliday的销售分布（Sales&gt;=15000）</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'SchoolHoliday'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">[</span>data_train_new<span class="token punctuation">.</span>Sales<span class="token operator">&gt;=</span><span class="token number">15000</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'SchoolHoliday的销售分布（Sales&gt;=15000）'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'SchoolHoliday的销售分布（Sales&gt;=15000）'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192342279.png" alt="image-20211029192342279"></p> <p>学校是否放假对销售额的价格影响不大</p> <h2 id="_4-探索不同类型商店销售额情况"><a href="#_4-探索不同类型商店销售额情况" class="header-anchor">#</a> 4.探索不同类型商店销售额情况</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#StoreType</span>
fig<span class="token punctuation">,</span>ax<span class="token operator">=</span>plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#StoreType的销售分布</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'StoreType'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'StoreType的销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>barplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'StoreType'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'不同StoreType的平均每天每个商店销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192452665.png" alt="image-20211029192452665"></p> <p>对于StoreType而言，可以看出b类型销售额比其他的高，a,c,d类型基本没什么差距</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>fig<span class="token punctuation">,</span>ax<span class="token operator">=</span>plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#Assortment的销售分布</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Assortment'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'Assortment的销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>barplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Assortment'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'不同Assortment的平均每天每个商店销售分布'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192556931.png" alt="image-20211029192556931"></p> <p>对于Assortment，可以看出b类型最高，c比a类型高一些</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#顾客数与销售额的关系</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
data_train_new<span class="token punctuation">.</span>plot<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Customers'</span><span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'顾客数与销售额的关系'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210341274.png" alt="image-20211029210341274"></p> <p>整体来看顾客数和销售额正相关</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#探索是否营业与销售额关系</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token string">'Sales'</span><span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token string">'Open'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data_train_new<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token operator">**</span><span class="token punctuation">{</span><span class="token string">'title'</span><span class="token punctuation">:</span><span class="token string">'是否营业与销售额关系'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>Text<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'是否营业与销售额关系'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192624701.png" alt="image-20211029192624701"></p> <p>不营业没有销售额，符合正常逻辑</p> <h2 id="总结"><a href="#总结" class="header-anchor">#</a> <strong>总结：</strong></h2> <p>1.通过从不同时间维度探索销售额变化，可以看出药品需求是有强周期性的，较明显为10-12月份，因此每年在10月份前需要备好库存才能保证销售顺利。</p> <p>2.通过对促销相关特征探索销售额不同，发现对于那些高销售额的的店基本不会受当天促销影响，说明这类店这类店经营的是忠诚客户，关注更多的是质量非价格。所以这些店的经营模式值得那些大型的店作为参考。而一般的店促销则是一个吸引顾客比较重要的手段。</p> <p>3.通过对国家节假日特征探索发现，节假日大部分店关闭，而如果坚持营业，由于供给变少，此时这些点能获得比以往更高的销售额。</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/rossmann/guide/vue-press.html" class="prev">
        vue-press入门
      </a></span> <span class="next"><a href="/rossmann/guide/数据挖掘 二.html">
        前言
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/rossmann/assets/js/app.4768928e.js" defer></script><script src="/rossmann/assets/js/2.46bed51d.js" defer></script><script src="/rossmann/assets/js/12.d00b63a5.js" defer></script>
  </body>
</html>
